package tdd.customer.presentation;

import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.core.api.dataset.ExpectedDataSet;
import com.github.database.rider.spring.api.DBRider;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tdd.customer.application.CustomerService;
import tdd.customer.domain.customer.Customer;
import tdd.customer.domain.customer.CustomerRepository;

import java.util.List;

@DBRider
@SpringBootTest
@RunWith(SpringRunner.class)
public class CustomerRepositoryIT {
    @Autowired
    private CustomerService service;

    @Autowired
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    private CustomerRepository repository;

    @Test
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "expected-customers.yml", ignoreCols = "id")
    public void create_should_create_specific_customer() {
        repository.save(new Customer("张", "三"));
    }

    @Test
    @DataSet(value = "customers.yml", cleanBefore = true)
    public void findById_should_retrieve_specific_customer_by_id() {
        Customer actual = repository.findById(1L).orElseGet(() ->
                Assertions.fail("不存在id为1的客户"));
        Customer expected = new Customer("张", "三");
        expected.setId(1L);
        Assertions.assertThat(actual).isEqualToComparingFieldByField(expected);
    }

    @Test
    @DataSet(value = "customers.yml", cleanBefore = true)
    public void findAll_should_retrieve_all_customer() {
        List<Customer> actual = repository.findAll();
        Assertions.assertThat(actual).hasSize(2)
                .extracting("firstName", "lastName")
                .contains(Assertions.tuple("张", "三"),Assertions.tuple("李", "四"));
    }
}
